package middol.utils

import com.google.common.collect.MapMaker
import groovy.util.logging.Slf4j

//import com.ocse.base.service.action.database.Database
//import org.apache.tomcat.jdbc.pool.DataSource
//import org.apache.tomcat.jdbc.pool.PoolProperties

import java.util.concurrent.ConcurrentMap

@Slf4j
class DBUtil {

//    private static ConcurrentMap<Long, DataSource> concurrentLinkedHashMap = new MapMaker().makeMap()
//
//    static {
//        concurrentLinkedHashMap = new MapMaker().makeMap()
//    }
//
//    static DataSource getDataSource(Database database) {
//        DataSource datasource = getOracleDataSource(database)
//        return datasource
//    }
//
//    static DataSource getOracleDataSource(Database database) {
//        DataSource dataSource = concurrentLinkedHashMap.get(database?.sys_database_id)
//        if (dataSource == null) {
//            PoolProperties p = new PoolProperties()
//            String name = database.dbuser
//            String passWord = database.dbpassword
//            if ("sys".equalsIgnoreCase(name) || "system".equalsIgnoreCase(name)) {
//                name += " as sysdba"
//            }
//            p.setUrl(database.dburl)
//            p.setDriverClassName("oracle.jdbc.driver.OracleDriver")
//            p.setUsername(name)
//            p.setPassword(passWord)
//            p.setJmxEnabled(true)
//            p.setTestWhileIdle(false)
//            p.setTestOnBorrow(true)
//            p.setValidationQuery("SELECT 1 FROM DUAL")
//            p.setTestOnReturn(false)
//            p.setValidationInterval(30000)
//            p.setTimeBetweenEvictionRunsMillis(30000)
//            p.setMaxActive(100)
//            p.setInitialSize(10)
//            p.setMaxWait(10000)
//            p.setRemoveAbandonedTimeout(60)
//            p.setMinEvictableIdleTimeMillis(30000)
//            p.setMinIdle(10)
//            p.setLogAbandoned(true)
//            p.setRemoveAbandoned(true)
//            p.setJdbcInterceptors(
//                    "org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;" +
//                            "org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer")
//            dataSource = new DataSource(poolProperties: p)
//            concurrentLinkedHashMap.put(database?.sys_database_id, dataSource)
//        }
//        dataSource
//    }

    /**
     * 组装分页语句
     * @param sql
     * @param params
     * @return
     */
    def static getOraclePageSQL(def sql, def params) {
        def sqlPage = sql
        if (params.max != null && params.offset != null) {
            //oracle
//            sqlPage = """
//                                select *
//                                  from (select a.*, rownum as rnum
//                                          from (${sql}) a
//                                         where rownum <=${params.max.toInteger() + params.offset.toInteger()})
//                                 where rnum > ${params.offset.toInteger()}
//                             """
            // mysql
            sqlPage = """
                select P.* from (SELECT @rowno:=@rowno+1 as rowno, O.* from (${sql}) O,(select @rowno:=0) t) P where rowno between ${params.offset.toInteger()} and ${params.max.toInteger() + params.offset.toInteger()}
            """
        }
        return sqlPage
    }

    /**
     * 获取总数
     * @param sql
     * @return
     */
    def static getOracleCountSQL(def sql) {
        def sqlPage = """
                            select count(*)
                              from (${sql})
                         """
        return sqlPage
    }
}
